package com.maple.utils.util;

import android.os.Message;
import android.text.TextUtils;

public class MyUtils {

    /**
     * 将byte数组转换成string类型表示
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder builder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        String hv;
        for (int i = 0; i < src.length; i++) {
            // 以十六进制（基数 16）无符号整数形式返回一个整数参数的字符串表示形式，并转换为大写
            hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
            if (hv.length() < 2) {
                builder.append(0);
            }
            builder.append(hv);
        }

        return builder.toString();
    }

    /**
     * 将Hex String转换为Byte数组
     *
     * @param hexString the hex string
     * @return the byte [ ]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (TextUtils.isEmpty(hexString)) {
            return null;
        }
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() >> 1];
        int index = 0;
        for (int i = 0; i < hexString.length(); i++) {
            if (index  > hexString.length() - 1) {
                return byteArray;
            }
            byte highDit = (byte) (Character.digit(hexString.charAt(index), 16) & 0xFF);
            byte lowDit = (byte) (Character.digit(hexString.charAt(index + 1), 16) & 0xFF);
            byteArray[i] = (byte) (highDit << 4 | lowDit);
            index += 2;
        }
        return byteArray;
    }

    public static String dealWeightOld(byte[] buffer) {
        try {
            byte[] buffer_new = new byte[1024];

            int num = 290;
            int n = 0;

            for (int i = 0; i < num; i++) {
                if ((buffer[i] == 0x0d) && (buffer[i + 1] == 0x0a)) {
                    buffer_new[n] = 0x0a;
                    i++;
                } else {
                    buffer_new[n] = buffer[i];
                }
                n++;
            }

            String s = new String(buffer_new, 0, n);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
